import { getToken, setToken } from "@/utils/auth";
import http from "@/utils/http";
import { customersMe, guestCarts, cartsMine } from "@/api/rest_api";
let axios = require("axios");

const state = {
  token: getToken(),
  cardCode: "",
  firstname: "",
  lastname: "",
  email: "",
  default_billing: "",
  default_shipping: "",
  store_id: 0,
  website_id: 0,
  customer_id: 0,
  quote_id: 0, //购物车id
  addressList: []
};

const mutations = {
  SET_TOKEN: (state, token) => {
    state.token = token;
  },
  SET_CARDCODE: (state, cardCode) => {
    state.cardCode = cardCode;
  },
  SET_FIRSTNAME: (state, firstname) => {
    state.firstname = firstname;
  },
  SET_LASTNAME: (state, lastname) => {
    state.lastname = lastname;
  },
  SET_EMAIL: (state, email) => {
    state.email = email;
  },
  SET_DEFAULT_BILLING: (state, default_billing) => {
    state.default_billing = default_billing;
  },
  SET_DEFAULT_SHIPPING: (state, default_shipping) => {
    state.default_shipping = default_shipping;
  },
  SET_SHORE_ID: (state, store_id) => {
    state.store_id = store_id;
  },
  SET_WEBSITE_ID: (state, website_id) => {
    state.website_id = website_id;
  },
  SET_CUSTOMER: (state, customer_id) => {
    state.customer_id = customer_id;
  },
  SET_QUOTE_ID: (state, quote_id) => {
    state.quote_id = quote_id;
  },
  SET_ADDRESSLIST: (state, addressList) => {
    state.addressList = addressList;
  },
};

const actions = {
  // login
  Login({ commit }, params) {
    return new Promise((resolve, reject) => {
      http
        .post("integration/customer/token", params)
        .then(res => {
          if (res.error) return resolve({ data: false });
          commit("SET_TOKEN", res.res);
          setToken(res.res);
          resolve({ data: true });
        })
        .catch(() => {
          resolve({ data: false });
        });
    });
  },
  getInfo({ commit, state, dispatch }) {
    return new Promise((resolve, reject) => {
      let config = {
        method: "get",
        url: "http://47.119.123.183/rest/V1/carts/mine",
        headers: {
          Authorization: "Bearer " + state.token
        },
        data: ""
      };
      axios(config)
        .then(Response => {
          // console.log(Response);
          let customer = Response.data.customer;
          	commit("SET_DEFAULT_BILLING", customer.default_billing);
          	commit("SET_DEFAULT_SHIPPING", customer.default_shipping);
          	commit("SET_EMAIL", customer.email);
          	commit("SET_FIRSTNAME", customer.firstname);
          	commit("SET_LASTNAME", customer.lastname);
          	commit("SET_SHORE_ID", customer.store_id);
          	commit("SET_WEBSITE_ID", customer.website_id);
          	commit("SET_ADDRESSLIST", customer.addresses);
						resolve(Response.data.items)
        })
        .catch(error => {
					dispatch('loginOut')
        });
    });
  },
  // get card-code
  async getGuestCarts({ commit }) {
    let { error, res } = await guestCarts();
    if (error) return;
    commit("SET_CARDCODE", res);
  },
  async createCart({ commit, state }) {
    var params = JSON.stringify({
      customerId: state.customer_id,
      storeId: state.store_id
    });
    let { error, res } = await cartsMine(params, state.token);
    if (error) return;
    commit("SET_QUOTE_ID", res);
    // console.log(res)
  },
  loginOut({ commit }) {
    commit("SET_DEFAULT_BILLING", "");
    commit("SET_DEFAULT_SHIPPING", "");
    commit("SET_EMAIL", "");
    commit("SET_FIRSTNAME", "");
    commit("SET_LASTNAME", "");
    commit("SET_SHORE_ID", 0);
    commit("SET_WEBSITE_ID", 0);
    commit("SET_CUSTOMER", 0);
    commit("SET_TOKEN", "");
  }
};

export default {
  namespaced: true,
  state,
  mutations,
  actions
};
